package com.goodfox.web.generic.provide.util;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;

/**
 * 参数处理工具类
 * @author qin gt
 *
 */
public class ParameterUtil {

	/** log4j日志器 */
	private static Logger logger=Logger.getLogger(ParameterUtil.class);
	
	/**
	 * 获取request参数值
	 * @param request request对象
	 * @param paraName 参数名
	 * @return
	 */
	public static String getReqParaValue(HttpServletRequest request,String paraName){
		String paraValue="";
		try{
			if(request.getParameter(paraName)!=null){
				paraValue=request.getParameter(paraName).trim();
			}
		}catch(Exception ex){
			logger.debug("获取参数异常", ex);
		}
		return paraValue;
	}
	
	/**
	 * 获取request参数值（经过utf-8解码转换）
	 * @param request request对象
	 * @param paraName 参数名
	 * @return
	 */
	public static String getReqParaDecodeValue(HttpServletRequest request,String paraName){
		String paraValue="";
		try{
			if(request.getParameter(paraName)!=null){
				paraValue=URLDecoder.decode(request.getParameter(paraName), "UTF-8");
			}
		}catch(Exception ex){
			logger.debug("获取参数异常", ex);
		}
		return paraValue;
	}
	
	/**
	 * request参数将String转换成List（以sign为List元素分割点）
	 * @param request request对象
	 * @param paraName 参数名
	 * @return
	 */
	public static ArrayList<String> getReqParaListForString(HttpServletRequest request,String paraName,String sign){
		String str=getReqParaValue(request,paraName);
		return getListForString(str,sign);
	}
	
	/**
	 * 将String分割为List（以sign为List元素分割点）
	 * @param str 源数据
	 * @param sign 分割点
	 * @return
	 */
	public static ArrayList<String> getListForString(String str,String sign){
		ArrayList<String> al=new ArrayList<String>();
		try{
			if(str.length()>0){
				if(str.lastIndexOf(sign)!=str.length()-1){
					str+=sign;
				}
				if(str.indexOf(sign)>=0){
					boolean bWhile=true;
					while(bWhile){
						if(str.indexOf(sign)>=0){
							al.add(str.substring(0,str.indexOf(sign)).trim());
							str=str.substring(str.indexOf(sign)+1);
						}
						if(str.trim().equals("") || str.trim().length()==0){
							bWhile=false;
						}
					}
				}else{
					al.add(str);
				}
			}
		}catch(Exception ex){}
		return al;
	}
	
	/**
	 * 将request参数自动转换成Map对象
	 * @param request request对象
	 * @return
	 */
	public static Map getReqParaMap(HttpServletRequest request){
		Map map=new HashMap();
		Set<String> setKey = request.getParameterMap().keySet();
        for(String key:setKey) {
        	map.put(key, getReqParaValue(request,key));
        }
		return map;
	}

	/**
	 * 字符串补"0"
	 * @param str 源字符串
	 * @param strLength 目标位数
	 * @param isLeft 是否补左，true补左  false补右
	 * @return
	 */
	public static String append0ForNum(String str, int strLength,boolean isLeft) {
		int strLen = str.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer sb = new StringBuffer();
				if(isLeft){
					sb.append("0").append(str);
				}else{
					sb.append(str).append("0");
				}
				str = sb.toString();
				strLen = str.length();
			}
		}

		return str;
	}
	
	/**
	 * 获取单元格内容
	 * @param cell
	 * @param c
	 * @return
	 */
	public static String getCellValue(HSSFCell cell,Class c){
		String value=null;
		if(cell!=null){
			try{
				if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
					if(Double.class.equals(c)){
						value=String.valueOf(cell.getNumericCellValue()).trim();
					}else{
						value=String.valueOf((long)cell.getNumericCellValue()).trim();
					}
				}else if(cell.getCellType()==HSSFCell.CELL_TYPE_STRING){
					value=cell.getStringCellValue()==null?null:cell.getStringCellValue().trim();
				}else{
					if(Long.class.equals(c)){
						value=String.valueOf((long)cell.getNumericCellValue()).trim();
					}else if(Double.class.equals(c)){
						value=String.valueOf(cell.getNumericCellValue()).trim();
					}else{
						value=cell.getStringCellValue()==null?null:cell.getStringCellValue().trim();
					}
				}
				if(value.equals("")){
					value=null;
				}
			}catch(Exception ex){
				logger.error("获取单元格内容",ex);
				value=null;
			}
		}
		return value;
	}
	
	/**
	 * 判断是否为小数
	 * @param value
	 * @return
	 */
	public static boolean judgeDecimal(String value){
		return matchRegular("([1-9]+[0-9]*|0)(\\.[\\d]+)?",value);
	}
	
	/**
	 * 判断是否为整数
	 * @param value
	 * @return
	 */
	public static boolean judgeInteger(String value){
		return matchRegular("^[0-9]*$",value);
	}
	
	/**
	 * 验证正则表达式
	 * @param pattern
	 * @param value
	 * @return
	 */
	public static boolean matchRegular(String pattern,String value){
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(value);
		return m.find();
	}
}
